package org.eiahe.hr.interview.controller;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.interview.domain.InterviewQuestion;
import org.eiahe.hr.interview.domain.InterviewQuestionBank;
import org.eiahe.hr.interview.domain.vo.InterviewQuestionVO;
import org.eiahe.hr.interview.domain.dto.InterviewQuestionQueryDTO;
import org.eiahe.hr.interview.service.InterviewBehaviorQuestionsService;
import org.eiahe.hr.interview.service.InterviewQuestionBankService;
import org.eiahe.hr.interview.service.InterviewQuestionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 面试题信息表
 * @author :zhangnn
 * @className :InterviewQuestionBankController
 * @description: TODO
 * @date 2025-08-13 17:24:53
 */
@Slf4j
@AllArgsConstructor
@RestController
@RequestMapping("/interview/question")
public class InterviewQuestionController extends BaseController {

    private final InterviewQuestionService questionService;
    private final InterviewBehaviorQuestionsService behaviorQuestionsService;
    private final InterviewQuestionBankService interviewQuestionBankService;

    /**
     * 分页
     */
    @PostMapping("/page")
    public TableDataInfo<InterviewQuestionVO> list(InterviewQuestionQueryDTO queryDTO, PageQuery pageQuery) {
        //行为测试的场合
        TableDataInfo<InterviewQuestionVO> page = new TableDataInfo<>();
        if(null != queryDTO.getInterviewType() && queryDTO.getInterviewType() == 2){
            page = behaviorQuestionsService.selectBehaviorQuestions(queryDTO,pageQuery);
        } else {
            page = questionService.selectQuestionBankPage(queryDTO,pageQuery);
        }
        return page;
    }

    /**
     * 通过ID查询
     */
    @GetMapping("/{id}/{bankId}")
    public R<InterviewQuestion> getById(@PathVariable Long id, @PathVariable Long bankId) {
        InterviewQuestionBank bank = interviewQuestionBankService.getById(bankId);
        if(bank.getInterviewType() == 2){ //行为分析
            return  R.ok(questionService.queryBehaviorQuestionById(id,bank.getBankName()));
        }
        return R.ok(questionService.queryById(id));
    }

    /**
     * 新增题库信息
     * @param question
     * @return
     */
    @Log(title = "新增题目", businessType = BusinessType.INSERT,linkType = "interview",nameField = "questionTitle")
    @PostMapping("/create")
    public R<Void> add(@Validated @RequestBody InterviewQuestion question) {
        InterviewQuestionBank bank = interviewQuestionBankService.getById(question.getBankId());
        if(bank.getInterviewType() == 2){ //行为分析
            questionService.insertBehaviorQuestion(question);
            return  R.ok("新增行为分析题目成功");
        }
        questionService.insertQuestion(question);
         return R.ok("新增题目成功");
    }

    /**
     * 修改
     */
    @Log(title = "修改题目", businessType = BusinessType.UPDATE,linkType = "interview",nameField = "questionTitle")
    @PostMapping("/update")
    public R<Void> update(@Validated @RequestBody InterviewQuestion question) {
        InterviewQuestionBank bank = interviewQuestionBankService.getById(question.getBankId());
        if(bank.getInterviewType() == 2){ //行为分析
            boolean success = questionService.updateBehaviorQuestion(question);
            return success ? R.ok("更新行为分析题目成功") : R.fail("更新行为分析题目失败");
        }

        boolean success = questionService.updateQuestion(question);
        return success ? R.ok("更新成功") : R.fail("更新失败");
    }

    /**
     * 删除
     */
    @Log(title = "删除题目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}/{bankId}")
    public R<Boolean> remove(@PathVariable Long id,@PathVariable Long bankId) {
        InterviewQuestionBank bank = interviewQuestionBankService.getById(bankId);
        if(bank.getInterviewType() == 2){ //行为分析
            boolean success = questionService.deleteBehaviorQuestion(id);
            return success ? R.ok("删除行为分析题目成功") : R.fail("删除行为分析题目失败");
        }
        boolean success = questionService.deleteQuestion(id);
        return success ? R.ok("删除成功") : R.fail("删除失败");
    }

    /**
     * 生成面试题目
     * @param bankId
     * @param level
     * @return
     */
    @Log(title = "生成面试题目", businessType = BusinessType.INSERT)
    @PostMapping("/generateInterviewQuestions/{bankId}/{level}")
    public R<List<InterviewQuestion>> generateInterviewQuestions(@PathVariable("bankId") Long bankId,
                                                                     @PathVariable("level")  Integer level){
        if(null == level){
            return R.fail("请先录入面试职位等级：初级-中级-高级");
        }
        InterviewQuestionBank bank = interviewQuestionBankService.getById(bankId);
        if(bank.getInterviewType() == 2){
            return R.ok(behaviorQuestionsService.selectBehaviorQuestionsList(bankId,level));
        } else {
            return R.ok(questionService.generateInterviewQuestions(bankId,level));
        }
    }

}
